﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using ADA.Entity;
using System.Data;
using System.Data.SqlClient;

namespace ADA.Data
{
    public class DALAdmin
    {
        //private string conStr = ConfigurationManager.AppSettings["connectionString"];
        private string conStr = Common.CONNECTION_STRING;

        public Admin getAdminInfo(string name)
        {
            Admin ad = new Admin();
            DataTable tblAdminInfo = new DataTable();

            string sql = @"SELECT   [Admin].[ID],
                                    [Admin].[Password]
                            FROM    [Admin]
                            WHERE [Admin].[Name] = @name";

            SqlConnection conn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(tblAdminInfo);

            if (tblAdminInfo.Rows.Count > 0)
            {
                ad.ID = Convert.ToInt16(tblAdminInfo.Rows[0]["ID"]);
                ad.Password = Convert.ToString(tblAdminInfo.Rows[0]["Password"]).Trim();
            }

            return ad;
        }

        public void updatePass(short adminID, string newPass)
        {
            string sql = @"UPDATE   [Admin]
                            SET     [Admin].[PassWord] = @password 
                            WHERE   [Admin].[ID] = @adminID";

            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = newPass;
            cmd.Parameters.Add("@ID", SqlDbType.TinyInt).Value = adminID;

            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public DataTable getAllAdmin()
        {
            DataTable tblAdminInfo = new DataTable();

            string sql = @"SELECT   [Admin].[ID],
                                    [Admin].[Name],
                                    [Admin].[Password],
                                    [Admin].[Email],
                                    [Admin].[Phone]
                            FROM    [Admin]
                            WHERE [Admin].[Name] != @name";

            SqlConnection conn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = "superadmin";

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(tblAdminInfo);
            return tblAdminInfo;
        }

        public void insertAdmin(Admin ad)
        {
            string sql = @"INSERT INTO  [Admin]
                                        ([Name]
                                        ,[Password]
                                        ,[Email]
                                        ,[Phone])
                                 VALUES
                                        (@name
                                        ,@password
                                        ,@email
                                        ,@phone)";

            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ad.Name;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = ad.Password;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = ad.Email;
            cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = ad.Phone;

            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public void deleteAdmin(short adminID)
        {
            string sql = @"DELETE FROM  [Admin]
                                WHERE   [Admin].[ID] = @adminID";

            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.Add("@adminID", SqlDbType.TinyInt).Value = adminID;

            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
}
